home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
dskut
/
chop1292.zip
/
JOIN.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-12-18
|
16KB
|
506 lines
/* This program joins the smaller files created by the CHOP program
back into the original input file
USAGE: JOIN FILENAME [INPATH] -SWITCHES
Version 1.02. Written by Edgar Swank using some code by W. J. Kennamer
and released into the public domain.
Note: this source is for IBM/MS C Compiler.
Compile with HUGE memory model /AH.
FILENAME is any valid MS-DOS filename. Wildcards are not supported.
This will be the single output filename.
Input file names will be FILENAME.1, FILENAME.2, etc.
Program will stop when FILENAME.n is not found, or when Esc is entered
in response to prompt from -w option.
Terminating <ctrl>Z is removed from Input files
UNLESS -b is specified.
Terminating <ctrl>Z is added to Output files <11/7/91>
ONLY if -e is specified AND -b is NOT specified.
INPATH is an optional disk/pathname (A: or \path\ or a:\path\)
to get input files on a different disk and/or directory from output file.
SWITCHES must follow the FILENAME and INPATH parameters,
and may be entered in any order. Switches may be combined.
Valid SWITCHES are:
-b binary ║ do not remove ^z from input files.
-e eof ║ add ^z to output file.
║ Note: may interfere with appending
║ to output file.
-w wait ║ wait before opening each input file.
║ can change input diskette if separate from output.
File pointer fp1 is the output file. fp2 is always the input
file, though its name changes as new input files are
opened.
*/
#include <fcntl.h>
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#define VERSION "1.02"
#define PROGNAME "JOIN"
#define CTRL_Z 0x1a
#define VOID int
#define CR 0x0d
#define LF 0x0a
#define ESC 0x1b
#define OFF 0
#define ON 1
int binary = OFF; /* do not remove EOF */
int wait = OFF; /* Wait before each input file open */
int weof = OFF; /* Write EOF ^z to output file */
int c; /* current character */
int j; /* index */
int i,u;
long inpleft; /* Bytes input left to read */
long inplimit; /* Bytes input left to read low lim */
long bfsiz; /* I/O buffer size */
long bread; /* Bytes read into buffer */
long pbread; /* Bytes read this partition */
long bwrit; /* Bytes written from buffer */
long pbwrit; /* Bytes written this partition */
long size = 0; /* partition value -- file size */
long byte_count = 0; /* total bytes in original file */
long inpfz = 0; /* total bytes in original file */
long out_file_size = 0; /* total bytes in output file */
char *bfpa[20]; /* I/O buffer pointer array */
FILE *fp1,*fp2; /* fp1=output, fp2=input */
int derno,erno;
int bfx; /* buffer array index */
int num_input_files = 0; /* no of files successfully read */
int length; /* length of matching string */
int int_size; /* integer for file size */
int nmb; /* number of 16k blocks IO */
unsigned int w;
char *p; /* pointer to switch string */
char *period_p; /* location of period in filename */
char *input_file; /* input file name */
char filename[80]; /* original file name */
char fnwork[80]; /* workarea for file name */
char outfilename[80]; /* output file name */
char inpath[64]; /* optional input disk/path */
char line1[80]; /* data buffer */
char line2[30]; /* data buffer */
char *switches; /* string to hold switches */
VOID help();
VOID close_out();
VOID delete_files();
VOID block_read();
VOID block_write();
VOID copy_to_eol();
VOID construct_input_filename();
VOID Open_Next_Input_File();
VOID set_switches();
main(argc,argv)
int argc;
char *argv[];
{
/* test code to prints args **********
printf("ARGC=%d\n",argc);
for (j=0;j<argc ;j++ )
{
printf("ARGV[%d]=%s\n",j,argv[j]);
}
***************************************/
bfsiz=0;
for (nmb=1;nmb<=20 ;nmb++ )
{
bfpa[nmb-1]=malloc(16384);
if (bfpa[nmb-1]==NULL)
{
break;
}
else
{
bfsiz=(long)nmb*16384;
}
} /*for (nmb=1;nmb<=10 ;nmb++ )*/
nmb--;
if (nmb==0)
{
printf ("Insufficient Memory for I/O Buffer.\n");
exit(8);
}
else
printf ("%ld (%u*16384) bytes allocated for I/O Buffers.\n",bfsiz,nmb);
if( argc > 4 || argc < 2 || strcmp(argv[1],"-?")==0
|| strcmp(argv[2],"-?")==0
|| strcmp(argv[3],"-?")==0 )
{
help();
exit(1);
}
/* see which argument is the switch */
switches = line2;
strcpy(switches,"");
for (j=2;j<argc;j++)
{
if( argv[j][0]=='-')
{
strcpy(switches,argv[j]);
break;
}
} /*for*/
if (j>2)
strcpy(inpath,argv[2]);
else
inpath[0]=0;
set_switches();
strcpy(filename,argv[1]);
strcpy(outfilename,argv[1]);
period_p = strchr(filename,'.'); /* locate the period, if any */
if( period_p != NULL )
*period_p = '\0'; /* chop off the period */
if (inpath[0]!=0) /* if inpath specified */
construct_input_filename();
Open_Next_Input_File();
if( (fp1 = fopen(argv[1],"wb") ) == NULL)
{
printf("Cannot open %s for output.\n",argv[1]);
printf("Exiting the program.\n");
exit(1);
}
else
printf ("Output file Open\n");
if (binary==ON)
inplimit=0;
else
inplimit=3;
bread=0;pbread=0;w=16384;
bwrit=0;pbwrit=0;bfx=0;
while (1)
{
while (inpleft>inplimit)
{
block_read();
block_write();
if( inpleft == 0 )
{
Open_Next_Input_File();
}
bfx=0;bread=0;w=16384;
bwrit=0;
} /*while (inpleft>inplimit)*/
while(1)
{
c = getc(fp2);
if( c == EOF )
{
fclose(fp2);
break;
}
inpleft--;
if (binary==ON || c != CTRL_Z)
{
putc(c,fp1);
}
} /*while(1)*/
Open_Next_Input_File();
} /*while (1) (Exits thru Open-Next_Input_File)*/
} /*main(argc,argv)*/
/***************************************************************************/
VOID Open_Next_Input_File()
{
(num_input_files)++;
if(num_input_files > 999)
{
printf("Too many files--%d.\nExiting program\n.",num_input_files);
exit(1);
}
input_file = line1; /* initialize input_file pointer */
/* create the next filename */
sprintf(input_file,"%s.%-d",filename,num_input_files);
while (1)
{
if (wait==ON)
{
while (1)
{
printf("Ready to read File %s. \n\
Press Return to continue or Esc to Stop\n",input_file);
c=getch();
if (c==ESC)
{
close_o